package cc.fs.zhihuapp.ui.activity.helper;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.squareup.leakcanary.RefWatcher;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import cc.fs.zhihuapp.App;
import cc.fs.zhihuapp.AppConfig;
import cc.fs.zhihuapp.R;

/**
 * Created by fostion on 2015/9/7.
 */
public abstract class BaseActivity extends AppCompatActivity {

    private ProgressDialog waitDialog;
    public BaseActivity _this;
    public MyHandler handler = new MyHandler(this);
    private String themeStyle;
    private String fontStyle;

    public static ArrayList<Handler> handlers = new ArrayList<>();

    public static void addHandler(Handler handler) {
        handlers.add(handler);
    }

    public static void deleteHandler(Handler handler) {
        handlers.add(handler);
    }

    public static void removeHandler(Handler handler) {
        handlers.remove(handler);
    }

    public static void broacst(Message message) {
        if (handlers != null)
            for (Handler handler : handlers) {
                Message msg = new Message();
                msg.copyFrom(message);
                handler.handleMessage(msg);
            }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getCurrentThemeStyle().equals(AppConfig.VALUE_NIGHT)) {
            setTheme(R.style.AppTheme_Night);
            setActivityFontStyle();
        } else {
            setTheme(R.style.AppTheme);
            setActivityFontStyle();
        }
        //设置dialog
        _this = this;
        addHandler(handler);
        setUpWaitDialog();
    }

    public String getCurrentThemeStyle() {
        if (TextUtils.isEmpty(themeStyle)) {
            themeStyle = AppConfig.get(AppConfig.KEY_THEME);
        }
        return themeStyle == null ? "":themeStyle;
    }

    public String getCurrentFontStyle() {
        if (TextUtils.isEmpty(fontStyle)) {
            fontStyle = AppConfig.get(AppConfig.KEY_FONTSTYLE);
        }
        return fontStyle == null? "":fontStyle;
    }

    public void setActivityFontStyle() {
        if (getCurrentFontStyle().equals(AppConfig.VALUE_FONT_BIG)) {
            getTheme().applyStyle(R.style.FontStyle_Large, true);
        } else if (getCurrentFontStyle().equals(AppConfig.VALUE_FONT_MEDIUM)) {
            getTheme().applyStyle(R.style.FontStyle_Medium, true);
        } else {
            getTheme().applyStyle(R.style.FontStyle_Small, true);
        }
    }


    public void setThemeStyle(String mCurrentStyle) {
        this.themeStyle = mCurrentStyle;
    }

    @Override

    public void finish() {
        removeHandler(handler);
        super.finish();
    }

    public void sendMessage(Message message) {
        Message msg = new Message();
        msg.copyFrom(message);
        handler.sendMessage(msg);
    }

    public void runDelay(Runnable obj, int delayMillis) {
        Message msg = new Message();
        msg.obj = obj;
        WeakReference<Runnable> outer = new WeakReference<Runnable>(obj);

        msg.obj = outer;
        msg.what = -101;
        msg.arg1 = -102;
        msg.arg2 = -103;
        handler.sendMessageDelayed(msg, delayMillis);
    }

    public void T(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    public void L(String msg) {
        Log.e("---- activity log -----", msg);
    }

    public void T(int id) {
        Toast.makeText(this, getString(id), Toast.LENGTH_SHORT).show();
    }

    static class MyHandler extends Handler {

        private WeakReference<BaseActivity> mOuter;

        public MyHandler(BaseActivity baseActivity) {
            mOuter = new WeakReference<BaseActivity>(baseActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            final BaseActivity outer = mOuter.get();
            if (outer == null) {
                BaseActivity.deleteHandler(this);
            }else{

                if(msg.what == -101 && msg.arg1 == -102 && msg.arg2 == -103){
                    WeakReference<Runnable> objOuter = (WeakReference<Runnable>)msg.obj;
                    if(objOuter != null && objOuter.get() != null){
                        ((Runnable)objOuter.get()).run();
                    }
                    return;
                }
                outer.handler(msg);
            }
        }
    }

    public void setUpWaitDialog() {
        waitDialog = new ProgressDialog(this);
        waitDialog.setMessage("正在加载...");
        waitDialog.setCancelable(false);
    }

    public void showWaitProress() {
        if (waitDialog != null && !waitDialog.isShowing()) {
            waitDialog.show();
        }
    }

    public void hiddenWaitProgress() {
        if (waitDialog != null && waitDialog.isShowing()) {
            waitDialog.dismiss();
        }
    }

    public void reload() {
        Intent intent = getIntent();
        overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();

        overridePendingTransition(0, 0);
        startActivity(intent);
    }

    public abstract void handler(Message msg);

    @Override
    protected void onDestroy() {
        super.onDestroy();
        App.getRefWatcher().watch(this);
    }
}
